#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-11-06 2:33 下午
# @Author  : yangdy
# @File    : 2020-11-06
# @Software: PyCharmCE
from tools.my_methods import *


class Solution:
    def sortByBits(self, arr: List[int]) -> List[int]:
        if not arr:
            return []

        def cal(n):
            c = 0
            while n:
                c += n % 2
                n //= 2
            return c

        arr1 = list(map(lambda x: (x, cal(x)), arr))

        for i in range(len(arr1) - 1):
            for j in range(i + 1, len(arr1)):
                if arr1[i][1] > arr1[j][1] or (arr1[i][1] == arr1[j][1] and arr1[i][0] > arr1[j][0]):
                    arr1[i], arr1[j] = arr1[j], arr1[i]

        return list(map(lambda x: x[0], arr1))


questions = [
    ([1024, 512, 256, 128, 64, ], ),
    ([0, 1, 2, 3, 4, 5, 6, 7, 8], ),
]

work(questions, lambda q: Solution().sortByBits(q[0]))


